package com.playball.lottery.frm.buyhall;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ChildDTO;
import com.playball.lottery.customview.MyDialog;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.customview.YZJSAdapter2;
import com.playball.lottery.frm.yhzx.BetProtocolActivity;
import com.playball.lottery.frm.yhzx.JddRechargeListActivity;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.Constants;
import com.playball.lottery.util.ConvertJsonToList;
import com.playball.lottery.util.DefConstants;
import com.playball.lottery.util.JCUtil;
import com.playball.lottery.util.JCxhlbSortList;
import com.playball.lottery.util.LotConstants;
import com.playball.lottery.util.MyPreference;


/**
 * 竞彩足球购彩选号列表界面
 * 
 * @author Administrator
 * 
 */
public class YZJSxhlbActivity extends BaseActivity implements DefConstants {
	/** 上下文内容 */
	private Context context;
	private TextView moneyText;
	private TextView tipText;
	private EditText et_beishu;
	private TextView btnBuy;
	private Button btnChioce;
	private View btnDelAll;
	/** 发起合买 */
	TextView buy_jcbasxhlb_btn_hemai;
	View buy_jczqxhlb_linearlayout01;
	View buy_jczqxhlb_linearlayout02;
	private String issueName;
	private int bs;// 倍数
	private long allMoney;// 总金额
	private long count; // 注数
	private String passType;
	private int buy_icon;
	private int len; // 全局变量长度
	private int currLen;// 未删除的剩余条数
	private String Activity;
	private int a = 0;
	private ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

	boolean[] selected = new boolean[] { false, false, false, false, false, false, false };
	/** 竞彩足球比分过关方式 */
	boolean[] bfselected = new boolean[] { false, false, false, false };
	/** 奖金预测 */
	private TextView tvMyuce;
	/** 最小奖金预测 */
	static BigDecimal bdminM = new BigDecimal("0");
	/** 最大奖金预测 */
	static BigDecimal bdmaxM = new BigDecimal("0");
	/** 竞彩足球玩法 */
	private String strZQWF = "";
	/** 竞彩足球玩法数字ID */
	private int playTypeID;

	LinearLayout foot_layout;
	TextView tv_bet_protocol;
	CheckBox cb_bet_protocol;
	ListView listView;

	private String[] dyn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.context = this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.buy_yzjsxhlb);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
						| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

		// 获得传来的数组，并放入ListView
		Intent intent = getIntent();
		issueName = intent.getStringExtra("IssueName");
		strZQWF = intent.getStringExtra("strZQWF");

		ArrayList<ChildDTO> arrayList = (ArrayList<ChildDTO>) intent.getSerializableExtra("list_jcdto");
		String[] str = (arrayList.get(0).str_sp).split("/");
		a = str.length;
		len = arrayList.size();
		// 处理intent获取的数据
		for (ChildDTO child : arrayList) {
			String jc_mid = child.getmId();
			String nMm = child.getnMm();
			String str_hteam = child.gethTeam();
			String str_vteam = child.getvTeam();
			String str_sp = child.getStr_sp();
			list = getData(jc_mid, str_hteam, str_vteam, str_sp, child.geteTime());
		}
		// 初始化按钮
		initView();
		listView = (ListView) findViewById(R.id.lv_select_match);

		LayoutInflater inflater = LayoutInflater.from(this);
		foot_layout = (LinearLayout) inflater.inflate(R.layout.bet_protocol_foot, null);
		foot_layout.setPadding(0, 50, 0, 0);
		foot_layout.setGravity(Gravity.CENTER);
		tv_bet_protocol = (TextView) foot_layout.findViewById(R.id.bet_protocol);
		cb_bet_protocol = (CheckBox) foot_layout.findViewById(R.id.bet_protocol_choice);
		tv_bet_protocol.setText(Html.fromHtml("<u>我已阅读并同意《代购合买协议》"));
		cb_bet_protocol.setChecked(true);
		tv_bet_protocol.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivityForResult(new Intent(YZJSxhlbActivity.this, BetProtocolActivity.class), 0);
			}
		});
		listView.removeFooterView(foot_layout);
		listView.addFooterView(foot_layout);

		// 添加 说明
		/*
		 * RelativeLayout foot_layout2 = (RelativeLayout) inflater.inflate(
		 * R.layout.cup_ycjs_text, null); foot_layout2.setPadding(0, 30, 0, 0);
		 * listView.setSelector(R.color.transparent);
		 * listView.addFooterView(foot_layout2);
		 */

		// 添加 说明
		/*
		 * RelativeLayout foot_layout3 = (RelativeLayout) inflater.inflate(
		 * R.layout.cup_ycjs_text_zhu, null); foot_layout3.setPadding(0, 30, 0,
		 * 0); listView.setSelector(R.color.transparent);
		 * listView.addFooterView(foot_layout3);
		 */

		// //对选择的赛事进行排序,jc_mid
		ArrayList<HashMap<String, Object>> newlist = new ArrayList<HashMap<String, Object>>();
		newlist = JCxhlbSortList.SortListData(list);

		initBaseAdapter(newlist);

		// 购买按钮
		btnBuy.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub
				if (list.size() < 1) {
					Toast.makeText(YZJSxhlbActivity.this, R.string.betting_accounts_cannot_be_empty, Toast.LENGTH_SHORT)
							.show();
				} else if (et_beishu.getText().toString().equals("")) {
					Toast.makeText(YZJSxhlbActivity.this, R.string.worldcupfrm_2, Toast.LENGTH_SHORT).show();
					et_beishu.setText("10");
				} else if (Integer.valueOf(et_beishu.getText().toString()) < 10) {
					Toast.makeText(YZJSxhlbActivity.this, R.string.worldcupfrm_3, Toast.LENGTH_SHORT).show();
					et_beishu.setText("10");
				} else if (Integer.valueOf(et_beishu.getText().toString()) > 99999) {
					Toast.makeText(YZJSxhlbActivity.this, R.string.worldcupfrm_4, Toast.LENGTH_SHORT).show();
					et_beishu.setText("99999");
				} else {

					if (cb_bet_protocol.isChecked() == false) {
						Toast.makeText(YZJSxhlbActivity.this, R.string.need_to_agree_to_just_can_continue_to_buy,
								Toast.LENGTH_SHORT).show();
						return;
					}
					startBuyThread();
					/*
					 * MyDialog dialog = new MyDialog.Builder(
					 * YZJSxhlbActivity.this) .setTitle("购买提示")
					 * .setMessage("是否确认购买？") .setPositiveButton("确定", new
					 * DialogInterface.OnClickListener() { public void onClick(
					 * DialogInterface dialog, int which) { startBuyThread();
					 * dialog.cancel(); } }) .setNegativeButton("取消", new
					 * DialogInterface.OnClickListener() {
					 * 
					 * @Override public void onClick( DialogInterface dialog,
					 * int which) { dialog.cancel(); } }).create();
					 * dialog.setCanceledOnTouchOutside(false);
					 * dialog.setCancelable(true);// 设置back键是否可以取消
					 * dialog.show();
					 */
				}
			}
		});
	}

	public void onResume() {
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	private TextWatcher textWatcher = new TextWatcher() {
		private boolean isChanged = false;

		public void afterTextChanged(Editable s) {

			if (isChanged) {
				return;
			}
			// TODO Auto-generated method stub
			if (!et_beishu.getText().toString().equals("")) {
				bs = Integer.parseInt(s.toString());
				isChanged = true;
				String str2 = s.toString().trim().replaceFirst("^0*", "");// 正则去除
				if (bs < 1) {
					Toast.makeText(YZJSxhlbActivity.this, R.string.worldcupfrm_7, Toast.LENGTH_SHORT).show();
					et_beishu.setText("10");
					bs = 10;
				} else if (bs > 99999) {
					Toast.makeText(YZJSxhlbActivity.this, R.string.worldcupfrm_8, Toast.LENGTH_SHORT).show();
					et_beishu.setText("99999");
					bs = 99999;
				} else {
					et_beishu.setText(str2);
				}
				et_beishu.setSelection(et_beishu.length());
				isChanged = false;
				et_beishu.invalidate();

				Long m = Long.parseLong(allMoney + "") * Integer.parseInt(et_beishu.getText().toString());
				moneyText.setText(Html.fromHtml("共 " + "<font color=#fe5052>" + (2 * a * bs) + "</font>" + " 彩豆"));

			} else {
				et_beishu.setOnFocusChangeListener(new OnFocusChangeListener() {
					public void onFocusChange(View view, boolean hasFocus) {
						// TODO Auto-generated method stub
						if (hasFocus) {
							et_beishu.setText("");
						} else {
							if (et_beishu.getText().toString().equals("")) {
								Toast.makeText(YZJSxhlbActivity.this, R.string.buy_at_least_one_times,
										Toast.LENGTH_SHORT).show();
							}
						}
					}
				});
				bs = 0;
				moneyText.setText(Html.fromHtml("共 " + "<font color=#fe5052>" + 0 + "</font>" + " 彩豆"));
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

	};

	class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		String action;
		ShowTipDialog showdialog = new ShowTipDialog();

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(YZJSxhlbActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... paramsArr) {
			// 处理后台执行的任务，在后台线程执行
			String strResult = null;

			action = paramsArr[1];
			// 获取请求返回值
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(paramsArr[0], UserID, action, getApplicationContext());
			// 获取请求返回值
			return strResult;
		}

		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}

		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				if (action.equals("207")) {
					Buy(result);
				}
				showdialog.closeDialog();
			} else {
				Toast.makeText(YZJSxhlbActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}

		// 5.完成后操作
		protected void onCancelled() {
		}

	}

	private void startBuyThread() {
		ArrayList<ChildDTO> arrayList = ApplicationData.getInstance().getList_jcdto();
		String lotNum = getLotNum(arrayList);
		AsyncLoader task = new AsyncLoader();
		dyn = new String[2];
		JSONObject params = new JSONObject();
		try {
			// 设置请求所需参数
			JSONObject params0 = new JSONObject();
			params0.put("playid", 9005);
			params0.put("number", lotNum);
			params.put("SchemeType", 1);// 方案类型 1自购，2合买，3保存 1
			params.put("BetType", 6);// 投注类型 1选号投注 2单式上传 3先发后传 1
			params.put("BonusScale", 0);
			params.put("AssureShare", 0);
			params.put("Description", "");
			params.put("OpenUserList", "");
			params.put("SecrecyLevel", 4);
			params.put("BuyShare", bs * (2 * a));
			params.put("Multiple", bs);
			params.put("LotteryID", LotConstants.LOT_ID_JCZQ);
			params.put("IssueID", issueName);
			params.put("Number", "[" + params0.toString() + "]");
			params.put("Money", bs * (2 * a));
		} catch (JSONException ex) {
			// TODO Auto-generated catch block
			Log.e("error", ex.toString());
		}
		dyn[0] = params.toString();
		dyn[1] = "207";
		task.execute(dyn);
	}

	public void Buy(String strResult) {
		try {
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			if (Integer.parseInt(js.getString("Code")) < 0) {
				buy_icon = R.drawable.buy_failure;
				String balanceNotFull = js.getString("Data");
				// Log.d("van", js.getString("Code")+
				// "------------"+js.getString("Data"));
				if(js.getString("Data").contains(Constants.BALANCE_BUZU)){
					Intent intent = new Intent();
					intent.setClass(context, JddRechargeListActivity.class);
					intent.putExtra("params", dyn[0]);
					startActivityForResult(intent, REQ_CODE_NO_MONEY_2_RECHARGE);
				} else {
					new ShowTipDialog().showBuyInfo(YZJSxhlbActivity.this, balanceNotFull, "购买失败", buy_icon, dyn[0]);
				}
			} else {
				buy_icon = R.drawable.buy_success;
				String msg = "一场决胜" + "\n\n我的认购：" + (float) (bs * (2 * a)/*
																		 * -
																		 * canDeductMoney
																		 */) + "彩豆\n账户余额："
						+ ConvertJsonToList.getMap(js.getString("Data")).get("Balance").toString()/*
																								 * getBalance
																								 * (
																								 * )
																								 */+ "彩豆";
				new ShowTipDialog().showBuyInfo(YZJSxhlbActivity.this, msg, "投注成功", buy_icon, dyn[0]);

				// 存贮数据
				MyPreference.getInstance(this).storeBLotID(LotConstants.LOT_ID_JCZQ);
			}
		} catch (JSONException ex) {
			// 异常处理代码
			Log.e("error", ex.toString());
		}
	}

	public String getBalance() {
		String money = "";
		String strResult = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 设置请求所需参数
			JSONObject params = new JSONObject();
			// 设置请求action
			String action = "107";// 108获取用户基本信息
			// 获取请求返回值
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(params.toString(), UserID, action,
					getApplicationContext());
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			map = ConvertJsonToList.getMap(js.getString("Data"));
			money = map.get("Balance").toString();
		} catch (Exception ex) {
			// TODO Auto-generated catch block
			Log.e("error", ex.toString());
		}
		return money;
	}

	// 拼接投注号码
	public String getLotNum(List<ChildDTO> arrayList) {
		String lotNum = "";
		for (ChildDTO jcdto : arrayList) {
			lotNum += jcdto.getmId() + "=";

			// i作为一场比赛中选胜平负的个数
			String info = "";
			String sp = JCUtil.zhuanHuanSPGeShi(jcdto.getStr_sp());
			if (sp.contains("3")) {
				info += "3/";
			}
			if (sp.contains("0")) {
				info += "0/";
			}
			if (sp.contains("1")) {
				info += "1/";
			}
			info = info.substring(0, info.length() - 1);
			lotNum = lotNum + "6" + "_" + info + ",";
		}
		lotNum = lotNum.substring(0, lotNum.length() - 1);
		// System.out.println(lotNum);
		return lotNum;
	}

	// 初始化
	public void initView() {
		initText();

		buy_jczqxhlb_linearlayout01 = findViewById(R.id.buy_jczqxhlb_linearlayout01);
		buy_jczqxhlb_linearlayout02 = findViewById(R.id.buy_jczqxhlb_linearlayout02);

		btnBuy = (TextView) findViewById(R.id.bottom_right_btn);

		TextView info = (TextView) findViewById(R.id.top_title_play_method);

		info.setText("一场决胜");

		et_beishu = (EditText) findViewById(R.id.et_beishu);
		et_beishu.addTextChangedListener(textWatcher);

		bs = 10;
		et_beishu.setText("10");
		moneyText.setText(Html.fromHtml("共 " + "<font color=#fe5052>" + 0 + "</font>" + " 彩豆"));

		// 标题
		titleBar();
	}

	// 初始化TextView
	public void initText() {
		moneyText = (TextView) findViewById(R.id.bottom_tv_money);
		tipText = (TextView) findViewById(R.id.tv_tips_endtime);
		tvMyuce = (TextView) findViewById(R.id.tv_win_ranger);
	}

	// 获取页面显示list
	private ArrayList<HashMap<String, Object>> getData(String jc_mid, String str_hteam, String str_vteam, String jc_sp,
			String str_time) {
		// 拿到新的号码放到List
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("buy_jcxhlb_list_mid", jc_mid);
		map.put("buy_jcxhlb_list_hteam", str_hteam);
		map.put("buy_jcxhlb_list_vteam", str_vteam);
		map.put("buy_jcxhlb_list_sp", jc_sp);
		map.put("tip_time", str_time);
		list.add(map);
		return list;
	}

	// 初始化适配器
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void initBaseAdapter(ArrayList list) {

		YZJSAdapter2 yzjsAdapter2 = null;
		yzjsAdapter2 = new YZJSAdapter2(this, list, R.layout.yzjs_xhlb_item, new String[] { "buy_jcxhlb_list_mid",
				"buy_jcxhlb_list_hteam", "buy_jcxhlb_list_vteam", "buy_jcxhlb_list_sp" }, new int[] {
				R.id.tv_item_hteam, R.id.tv_item_vteam, R.id.btn_item_s, R.id.btn_item_p, R.id.btn_item_f });

		currLen = yzjsAdapter2.GetFootList().size();

		if (currLen != len) {
			bs = 10;
			allMoney = 0;
			et_beishu.setText("10");
			moneyText.setText(Html.fromHtml("共 " + "<font color=#fe5052>" + 0 + "</font>" + " 彩豆"));
		}
		if (currLen > 0) {
			list = new ArrayList<HashMap<String, Object>>();
			// 初始化全局JC变量
			ArrayList<ChildDTO> list_jcdto = new ArrayList<ChildDTO>();
			ApplicationData.getInstance().setList_jcdto(list_jcdto);

			for (ChildDTO jcdto : yzjsAdapter2.GetFootList()) {
				String jc_mid = jcdto.getmId();
				String str_team = jcdto.getStr_team();
				String str_hteam = jcdto.gethTeam();
				String str_vteam = jcdto.getvTeam();
				String str_sp = jcdto.getStr_sp();
				// System.out.println(str_sp);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("buy_jcxhlb_list_mid", jc_mid);
				map.put("buy_jcxhlb_list_team", str_team);
				map.put("buy_jcxhlb_list_hteam", str_hteam);
				map.put("buy_jcxhlb_list_vteam", str_vteam);
				map.put("buy_jcxhlb_list_sp", str_sp);
				list.add(map);
				ApplicationData.getInstance().getList_jcdto().add(jcdto);
			}
		} else {
			buy_jczqxhlb_linearlayout01.setVisibility(View.GONE);
			buy_jczqxhlb_linearlayout02.setVisibility(View.VISIBLE);
		}
		List<ChildDTO> studentList = new ArrayList<ChildDTO>();
		studentList = ApplicationData.getInstance().getList_jcdto();
		tipText.setText("共" + studentList.size() + "场比赛，截止时间：" + studentList.get(0).geteTime());
		int b = Integer.valueOf(et_beishu.getText().toString().trim());

		moneyText.setText(Html.fromHtml("共 " + "<font color=#fe5052>" + (2 * a * b) + "</font>" + " 彩豆"));

		listView.setAdapter(yzjsAdapter2);

		// YZJSAdapter yzjsAdapter = null;
		// yzjsAdapter = new YZJSAdapter(this, list, R.layout.jczq_dgp_item2,
		// new String[]{
		// "tv_matchname","tv_item_no_endtime", "buy_jcxhlb_list_hteam",
		// "buy_jcxhlb_list_vteam", "buy_jcxhlb_list_sp", "str_rqsp","rq2"
		// }, new int[]{
		// R.id.tv_item_match_name,R.id.tv_item_no_endtime,R.id.tv_item_hteam,R.id.tv_item_vteam,
		// R.id.ll1,R.id.btn_item_rq1,R.id.btn_item_s,R.id.btn_item_p,R.id.btn_item_f,
		// R.id.ll2,R.id.btn_item_rq2,R.id.btn_item_rqs,R.id.btn_item_rqp,R.id.btn_item_rqf,
		// });
		//
		// currLen = yzjsAdapter.GetFootList().size();
		//
		// if (currLen != len) {
		// bs = 10;
		// allMoney = 0;
		// et_beishu.setText("10");
		// moneyText.setText("共0彩豆");
		// }
		// if (currLen > 0) {
		// list = new ArrayList<HashMap<String, Object>>();
		// // 初始化全局JC变量
		// ArrayList<ChildDTO> list_jcdto = new ArrayList<ChildDTO>();
		// ApplicationData.getInstance().setList_jcdto(list_jcdto);
		//
		// for (ChildDTO jcdto : yzjsAdapter.GetFootList()) {
		// String jc_mid = jcdto.getmId();
		// String etime = jcdto.geteTime();
		// String str_team = jcdto.getStr_team();
		// String str_hteam = jcdto.gethTeam();
		// String str_vteam = jcdto.getvTeam();
		// String str_spspf = jcdto.getsPspf();
		// String str_sprqspf = jcdto.getsPRqspf();
		// String rq2 = jcdto.geteQ();
		// HashMap<String, Object> map = new HashMap<String, Object>();
		// map.put("tv_matchname", jc_mid);
		// map.put("tv_item_no_endtime", etime);
		// map.put("str_hteam", str_hteam);
		// map.put("str_vteam", str_vteam);
		// map.put("str_sp", str_spspf);
		// map.put("str_rqsp", str_sprqspf);
		// map.put("rq2", rq2);
		// list.add(map);
		// ApplicationData.getInstance().getList_jcdto().add(jcdto);
		// }
		// } else {
		// buy_jczqxhlb_linearlayout01.setVisibility(View.GONE);
		// buy_jczqxhlb_linearlayout02.setVisibility(View.VISIBLE);
		// }
		//
		// List<ChildDTO> studentList = new ArrayList<ChildDTO>();
		// studentList = ApplicationData.getInstance().getList_jcdto();
		// tipText.setText("共" + studentList.size() + "场比赛，截止时间："
		// + studentList.get(0).geteTime());
		// listView.setAdapter(yzjsAdapter);
		//

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == RESULT_OK) {
			if (requestCode == REQ_CODE_BUYMAIN || requestCode == REQ_CODE_NO_MONEY_2_RECHARGE) {
				ApplicationData.getInstance().resetJCData();
				setResult(110);
				finish();
			}
		}
	};

	// titleBar标题栏
	public void titleBar() {
		// Title信息(返回+Title)
		TextView back = (TextView) findViewById(R.id.top_title_back);
		back.setBackgroundResource(R.drawable.top_title_back_bg);
		back.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (currLen <= 0) {
					// 获得自定义的应用程序BallDTO,初始化
					ApplicationData.getInstance().resetJCData();
					setResult(RESULT_CANCELED);
					YZJSxhlbActivity.this.finish();
				} else {
					MyDialog.Builder dialog = new MyDialog.Builder(YZJSxhlbActivity.this);
					dialog.setTitle("退出提示");
					dialog.setMessage("退出后您的投注将不会保存，确认继续?");
					dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// 获得自定义的应用程序BallDTO,初始化
							ApplicationData.getInstance().resetJCData();
							setResult(RESULT_CANCELED);
							YZJSxhlbActivity.this.finish();
							dialog.cancel();
						}
					});
					dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					}).create().show();

				}
			}
		});
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			MyDialog.Builder dialog = new MyDialog.Builder(YZJSxhlbActivity.this);
			dialog.setTitle("退出提示");
			dialog.setMessage("退出后您的投注将不会保存，确认继续?");
			dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// 获得自定义的应用程序BallDTO,初始化
					ApplicationData.getInstance().resetJCData();
					setResult(RESULT_OK);
					YZJSxhlbActivity.this.finish();
					dialog.cancel();
				}
			});
			dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			}).create().show();
		} else {
			return super.onKeyDown(keyCode, event);
		}
		return true;
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
	};
}
